﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Web.Mvc;
using System.Data;
using Enring.Data;

namespace Enring.Controls
{

    public class DataFormControl
    {
        private StringBuilder sb = new StringBuilder();
		private XmlNode config = null;
        private ControllerContext viewContext;
        private ViewDataDictionary viewData;
        private DataSet defaultDataSet;

        private AppUIParser parser;

        /// <summary>
        /// Default Field CSS Style
        /// </summary>        
        public string FieldClass { get; set; }
        public string SectionClass { get; set; }
        public string FieldNodeName { get; set; }
        public string TabNodeName { get; set; }
        public string SectionNodeName { get; set; }
        public string FormNodeName { get; set; }
        
        public string RootPath { get; set; }
        public string SelectedTabID { get; set; }

        public DataFormControl(XmlNode node, ControllerContext vContext, ViewDataDictionary vData)
		{
            this.SectionClass = "subheader";
            this.FieldClass = "form-field";
            this.FieldNodeName = "Field";
            this.TabNodeName = "Tab";
            this.SectionNodeName = "Section";
            this.FormNodeName = "Form";

            this.RootPath = "/";

            if (node == null || vContext == null || vData == null)
				throw new NullReferenceException();
			viewContext = vContext;
            viewData = vData;
			config = node;
            parser = new AppUIParser(vContext, vData);
		}

		public string GetHtml()
		{
			sb.Clear();
            StringBuilder sbFormAttr = new StringBuilder();
            sbFormAttr.Append(XmlHelper.InsertAttributeWhenEmpty(config, "id", " id=\"{0}\""));
            //Render form attributes
            sb.AppendFormat("<div {0}>", sbFormAttr.ToString());

            //Render Form Title
            sb.AppendFormat("<h1 class=\"mainbox-title\">{0}</h1>\n",UI.Localize(XmlHelper.GetAttributeValue(config,"title")));

            sb.AppendLine("<div class=\"mainbox-body\">");
            
            //Render form tabs
            RenderTab(sb, config);

            //Render form body
            RenderForm(sb, config);
            
            sb.AppendLine("</div>");
            sb.AppendLine("</div>");
			return sb.ToString();
		}

        public string GetHtml(string guid)
        {
            if (guid == null) return GetHtml();

            XmlNode node = config.SelectSingleNode(string.Format(".//*[@guid='{0}']",guid));
            if (node != null)
            {
                StringBuilder sb = new StringBuilder();
                string name = node.Name.ToLower().Trim();
                if (name.Equals("view"))
                {                    
                    this.RenderView(sb, node);
                }
                else if (name.Equals("fieldset"))
                {
                    this.RenderFieldset(sb, null, node);
                }
                else if (name.Equals("field"))
                {
                    this.RenderField(sb, node);
                }

                return sb.ToString();
            }
            else
                return string.Empty;
        }


        #region Utility functions

        /// <summary>
        /// Get the defaul DataSet
        /// </summary>
        /// <returns></returns>
        private DataSet getDataSet()
        {
            if (defaultDataSet != null)
                return defaultDataSet;
            if (viewData[Const.DefaultDataSetKey] != null)
                return viewData[Const.DefaultDataSetKey] as DataSet;
            else
            {
                XmlNode defaultDataSourceNode = config.SelectSingleNode(".//DataSource[@default='true']");
                if (defaultDataSourceNode != null)
                { 
                    DataMapping dm = new DataMapping(this.viewContext.HttpContext);
                    defaultDataSet = dm.SetDataset(defaultDataSourceNode, viewData);
                    return defaultDataSet;
                }
                else
                    return null;
            }
        }

        /// <summary>
        /// Get the DataSet by name
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        private DataSet getDataSet(string key)
        {
            if (viewData.Keys.Contains(key))
            {
                //Only one traslation
                return viewData[key] as DataSet;                
            }
            else
            {
                XmlNode dataSourceNode = config.SelectSingleNode(string.Format(".//DataSource[@name='{0}']",key));
                if (dataSourceNode != null)
                {
                    DataMapping dm = new DataMapping(this.viewContext.HttpContext);
                    return dm.SetDataset(dataSourceNode, viewData);
                }
                else
                    return null;
            }
        }

        private string GetValueFromDataSource(XmlNode node)
        {
            string defaultValue = XmlHelper.GetAttributeValue(node.SelectSingleNode("UI"), "default","");
            return GetValueFromDataSource(node, parser.GetValueFromFormat(null, defaultValue));
        }

        /// <summary>
        /// Get data value from datasource
        /// </summary>
        /// <param name="node"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        private string GetValueFromDataSource(XmlNode node,string defaultValue)
        {
            
            DataTable dt = GetDataTableFromDataSource(node);
            
            //Get data from the first row
            if (dt != null && dt.Rows.Count > 0)
            {
                string fieldName = XmlHelper.GetAttributeValue(node, "field");
                if (dt.Columns.IndexOf(fieldName) > -1)
                {
                    return dt.Rows[0][fieldName].ToString();
                }
                else
                    return defaultValue;
            }
            else
                return defaultValue;
        }

        /// <summary>
        /// Get data table from data source
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        private DataTable GetDataTableFromDataSource(XmlNode node)
        {
            string dataSource = XmlHelper.GetAttributeValue(node, "source");
            DataSet ds = null;
            DataTable dt = null;           
            if (dataSource.Length == 0)
                ds = getDataSet();
            else
                ds = getDataSet(dataSource);

            if (!DB.HasData(ds))
                return dt;

            string dataTable = XmlHelper.GetAttributeValue(node, "table");
            if (dataTable.Length == 0)
                //Get data from the first table
                dt = ds.Tables[0];
            else
            {
                if (ds.Tables[dataTable] != null)
                {
                    dt = ds.Tables[dataTable];
                }
            }
            return dt;
        }

        /// <summary>
        /// Check if a node is null and has the expected name nodeName
        /// </summary>
        /// <param name="node"></param>
        /// <param name="nodeName"></param>
        /// <returns></returns>
        private bool CheckNode(XmlNode node, string nodeName)
        {
            return node != null && node.Name.Equals(nodeName);
        }

        /// <summary>
        /// Check node's atttributeName and return the value of a node attribute or a defaultAttributeValue
        /// </summary>
        /// <param name="node"></param>
        /// <param name="nodeName"></param>
        /// <param name="attributeName"></param>
        /// <param name="defaultAttributeValue"></param>
        /// <returns></returns>
        private string GetNodeAttribute(XmlNode node, string nodeName, string attributeName, string defaultAttributeValue)
        {
            return GetNodeAttribute(node, nodeName, attributeName, defaultAttributeValue, null);    
        }

        /// <summary>
        /// Get Node Attribute value with a formated string.
        /// </summary>
        /// <param name="node"></param>
        /// <param name="nodeName"></param>
        /// <param name="attributeName"></param>
        /// <param name="defaultAttributeValue"></param>
        /// <param name="valueFormat">The format parameter must has only one paramter with the string of {0}</param>
        /// <returns></returns>
        private string GetNodeAttribute(XmlNode node, string nodeName, string attributeName, string defaultAttributeValue,string valueFormat)
        {
            if (CheckNode(node, nodeName))
            {
                if (node.Attributes[attributeName] != null)
                {
                    //valueFormat must has a "{0}" substring in it.
                    if (valueFormat != null && valueFormat.Length > 0 && valueFormat.IndexOf("{0}") > -1)
                        return string.Format(valueFormat,node.Attributes[attributeName].Value);
                    return node.Attributes[attributeName].Value;
                }
                else
                    return defaultAttributeValue;
            }
            else
                return defaultAttributeValue;
        }

        /// <summary>
        /// Get node value
        /// </summary>
        /// <param name="node"></param>
        /// <param name="nodeName"></param>
        /// <param name="xpath"></param>
        /// <param name="defaultAttributeValue"></param>
        /// <returns></returns>
        private string GetNodeValue(XmlNode node, string nodeName, string xpath, string defaultAttributeValue)
        {
            return GetNodeValue(node, nodeName, xpath, false, defaultAttributeValue);
        }

        /// <summary>
        /// Get node value from a node or a cdata type node
        /// </summary>
        /// <param name="node"></param>
        /// <param name="nodeName"></param>
        /// <param name="xpath"></param>
        /// <param name="isCDATA"></param>
        /// <param name="defaultAttributeValue"></param>
        /// <returns></returns>
        private string GetNodeValue(XmlNode node, string nodeName, string xpath, bool isCDATA, string defaultAttributeValue)
        {
            if (CheckNode(node, nodeName))
            {
                XmlNode tempNode = node.SelectSingleNode(xpath);
                if (tempNode != null)
                {
                    if (isCDATA && tempNode.ChildNodes.Count == 1)
                        return tempNode.ChildNodes[0].InnerText;
                    else
                        return tempNode.InnerText;
                }
                else
                    return defaultAttributeValue;
            }
            else
                return defaultAttributeValue;
        }

        #endregion


        /// <summary>
        /// Render Tab Node
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="node"></param>        
        protected void RenderTab(StringBuilder sb, XmlNode node)
        {
            //Add tabs
            XmlNodeList tabs = node.SelectNodes("Tabs/Tab");
            
            if (tabs.Count > 0)
            {
                sb.AppendFormat("<script type=\"text/javascript\" src=\"{0}Scripts/js/tabs.js\"></script>\n", RootPath);            
                sb.AppendLine("<div class=\"tabs cm-j-tabs cm-track\">\n\t<ul>");
                foreach (XmlNode tab in tabs)
                {
                    string id = GetNodeAttribute(tab, TabNodeName, "id", "");
                    sb.AppendFormat("<li id=\"{1}\" class=\"cm-js\"><a>{0}</a></li>",
                        GetNodeAttribute(tab,TabNodeName,"title","(Empty Tab)"),
                        id);
                    //If selected the tab then display tab with active style.
                }
                sb.AppendLine("\t</ul>\n</div>");                
            }
        }

        /// <summary>
        /// Render form body
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="node"></param>
        protected void RenderForm(StringBuilder sb, XmlNode node)
        {
            sb.AppendLine("<div class=\"cm-tabs-content\">");

            sb.AppendFormat("   <form action=\"{0}\" method=\"post\" name=\"{1}\" class=\"cm-form-highlight cm-disable-empty-files  cm-check-changes\" enctype=\"multipart/form-data\">\n ",
                XmlHelper.GetAttributeValue(node,"action"),//action
                XmlHelper.GetAttributeValue(node, "name") //name
                );
            //Add hidden fields
            sb.AppendFormat("<input type=\"hidden\" class=\"\" name=\"selected_section\" id=\"selected_section\" value=\"{0}\" />",
                viewContext.HttpContext.Request["selected_section"] != null ? viewContext.HttpContext.Request["selected_section"] : "");

            //Add tab divs
            XmlNodeList tabs = node.SelectNodes("Tabs/Tab");
            if (tabs.Count > 0)
            {
                int tabIndex = 0;
                foreach (XmlNode tab in tabs)
                {
                    sb.AppendFormat("<div class=\"{2}\" id=\"content_{0}\" style=\"display: {1};\"> \n",
                        GetNodeAttribute(tab, TabNodeName, "id", ""),//id
                        tabIndex == 0 ? "block" : "none",
                        XmlHelper.GetAttributeValue(tab, "class"));
                    RenderContent(sb, tab);
                    sb.AppendLine("</div>");
                    tabIndex++;
                }
            }
            else
            {
                //When no tabs found
                RenderContent(sb, node);
            }
            
            RenderButtonContainer(sb, node);

            sb.AppendLine("     </form>");
            sb.AppendLine("</div>");
        }

        private void RenderContent(StringBuilder sb, XmlNode node)
        {
            XmlNodeList sections = node.SelectNodes(".//Section");
            if (sections.Count > 0)
            {
                foreach (XmlNode section in sections)
                {
                    RenderSection(sb, section);
                }
            }
            else
            {
                XmlNodeList fieldsets = node.SelectNodes(".//FieldSet");
                foreach (XmlNode fieldset in fieldsets)
                {
                    RenderFieldset(sb,null, fieldset); 
                }
            }
        }

        protected void RenderSection(StringBuilder sb, XmlNode node)
        {            
            foreach (XmlNode childNode in node.ChildNodes)
            {
                RenderFieldset(sb, node, childNode);
            }            
        }

        private void RenderFieldset(StringBuilder sb, XmlNode sectionNode, XmlNode childNode)
        {
            StringBuilder fieldSetAttr = new StringBuilder();
            fieldSetAttr.Append(XmlHelper.InsertAttributeWhenEmpty(childNode, "guid", string.Format("id='{0}_{1}'", childNode.Name.ToLower().Trim(), "{0}")));
            sb.AppendFormat("<fieldset {0}>",fieldSetAttr.ToString());
            //Render Setion Form
            if (sectionNode != null)
            {
                sb.AppendFormat("   <h2 class=\"{0}\">\n", this.SectionClass);
                sb.AppendFormat("       {0}\n", UI.Localize(XmlHelper.GetAttributeValue(sectionNode, "title", "Empty")));
                sb.AppendFormat("   </h2>\n");
            }
            if (childNode.Name.Equals("FieldSet"))
            {
                //Render fields
                XmlNodeList fields = childNode.SelectNodes("Field");
                foreach (XmlNode field in fields)
                {
                    RenderField(sb, field);
                }

            }
            else if (childNode.Name.Equals("View"))
            {
                //Render View
                RenderView(sb, childNode);
            }
            sb.AppendLine("</fieldset>");
        }

        protected void RenderField(StringBuilder sb, XmlNode node)
        {
            sb.AppendFormat("<div class=\"{0} \">\n", this.FieldClass);
            string type = GetNodeAttribute(node.SelectSingleNode("UI"), "UI", "type", "").ToLower().Trim();
            switch (type)
            {
                case "singletext":
                    RenderSingleTextField(sb, node);
                    break;
                case "datetime":
                    RenderDateTimeField(sb, node);
                    break;
                case "multitext":
                    RenderMultiTextField(sb, node);
                    break;
                case "combox":
                    RenderComboxField(sb, node);
                    break;
                default:

                    break;
            }
            sb.AppendFormat("</div>");
        }

        protected void RenderButtonContainer(StringBuilder sb, XmlNode node)
        {
            sb.AppendLine("<div class=\"buttons-container cm-toggle-button buttons-bg\">");

            //Save Button
            sb.AppendFormat("<span  class=\"submit-button cm-button-main \"><input type=\"submit\" name=\"{1}\" value=\"{0}\" /></span>\n",
                        UI.Localize("Save"),
                        XmlHelper.GetAttributeValue(node,"name"));

			//Save And Close
            if(XmlHelper.GetAttributeValue(node,"show_saveandclose","false").Equals("true") && !parser.IsDialog())
            {
                sb.AppendFormat("&nbsp;<span  class=\"submit-button cm-button-main cm-save-and-close \"><input  class=\"cm-save-and-close \" type=\"submit\" name=\"{1}\" value=\"{0}\" /></span>",
                    UI.Localize("Save and close"),
                    XmlHelper.GetAttributeValue(node, "name"));
            }
            
            //Cancel Button
            StringBuilder cancelAttr = new StringBuilder();
            if(parser.IsDialog())
            {
                cancelAttr.AppendFormat(" class=\"{0}\"", "cm-dialog-closer cm-cancel tool-link");
            }
            else
            {
                cancelAttr.AppendFormat(" class=\"{0}\"", "underlined tool-link");
            }
            sb.AppendFormat("&nbsp;or&nbsp;&nbsp;<a {1}>{0}</a>",
                UI.Localize("Cancel"),
                cancelAttr.ToString()); //Cancel class

            //Add hidden fields
            string actionType = "insert";
            if (viewData[Const.DefaultDataID] != null)
            {
                actionType = "update";
                sb.AppendFormat("<input type=\"hidden\" name=\"{0}{1}\" value=\"{2}\" />",Const.FieldPrefix,Const.DefaultDataID,viewData[Const.DefaultDataID]);            
            }
            sb.AppendFormat("<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />",Const.SubmitActionType, actionType);
            sb.AppendFormat("<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />", Const.SubmitFormName, XmlHelper.GetAttributeValue(node,"name"));
            sb.AppendFormat("<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />", Const.SubmitFormGUID, XmlHelper.GetAttributeValue(node, "guid"));

            sb.AppendLine("</div>");		
        }

        #region Render Field Functions
        /// <summary>
        /// Create Single Text Field
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="node"></param>
        protected void RenderSingleTextField(StringBuilder sb, XmlNode node)
        {
            RenderFieldLabel(sb, node);
            string value = GetValueFromDataSource(node);
            string field = XmlHelper.GetAttributeValue(node, "field");

            XmlNode uiNode = node.SelectSingleNode("UI");

            sb.AppendLine("<span class=\"input-helper\">");
            sb.AppendFormat(" <input type=\"text\" name=\"{0}\" id=\"{1}\" size=\"{2}\" value=\"{3}\" class=\"{4}\" {5}/>\n",
                GenerateName(field),//name
                GenerateName(field),//id
                XmlHelper.GetAttributeValue(uiNode, "size", "15"),//size
                XmlHelper.GetAttributeValue(uiNode, "value", value),//value
                XmlHelper.GetAttributeValue(uiNode, "class", "input-text-large main-input"),//class，
                XmlHelper.GetAttributeValue(uiNode, "editable", "true").Equals("false") ? "disabled" : "" //disabled
                );
            sb.AppendLine("</span>");
            string tips = XmlHelper.GetNodeValue(node.SelectSingleNode("Tips"));
            if(tips.Length > 0)
                sb.AppendFormat("\"{0}\"", tips);
        }

        private void RenderFieldLabel(StringBuilder sb, XmlNode node)
        {
            sb.AppendFormat("<label {1}>{0}:</label>",
                GetNodeAttribute(node, FieldNodeName, "title", "(Empty)"),
                GetNodeAttribute(node, FieldNodeName, "must_fill", "false") == "false" ? "" : "class=\"cm-required\"");
        }

        protected void RenderMultiTextField(StringBuilder sb, XmlNode node)
        {
            RenderFieldLabel(sb, node);
            string value = GetValueFromDataSource(node);
            
            XmlNode uiNode = node.SelectSingleNode("UI");
            string field = XmlHelper.GetAttributeValue(node, "field");
            string id = XmlHelper.GetAttributeValue(node, "id", GenerateName(field));
            string name = XmlHelper.GetAttributeValue(node, "name", GenerateName(field));

            string rows = XmlHelper.GetAttributeValue(uiNode, "lines", "8");
            //Weather show the rich text editor or a plain textarea.
            string wysiwyg = XmlHelper.GetAttributeValue(uiNode, "richtext").Equals("true") ? "cm-wysiwyg" : "";
            string length = XmlHelper.GetAttributeValue(uiNode, "length","long");

            sb.AppendFormat("<textarea id=\"{0}\" name=\"{1}\" rows=\"{2}\" class=\"{4} input-textarea-{3}\">{5}</textarea>",
                id,
                name,
                rows,
                length,
                wysiwyg,
                value);
        }

        private static string GenerateName(string field)
        {
            return string.Format("{0}{1}",Const.FieldPrefix, field);
        }

        protected void RenderDateTimeField(StringBuilder sb, XmlNode node)
        {
            RenderFieldLabel(sb, node);
            
            XmlNode uiNode = node.SelectSingleNode("UI");
            string field = XmlHelper.GetAttributeValue(node, "field");
            string id = XmlHelper.GetAttributeValue(node, "id", GenerateName(field));
            string name = XmlHelper.GetAttributeValue(node, "name", GenerateName(field));
            string value = XmlHelper.GetAttributeValue(uiNode, "value", 
                DateTime.Now.ToString(XmlHelper.GetAttributeValue(uiNode,"dateFormat","yyyy/MM/dd")));
            string size = XmlHelper.GetAttributeValue(uiNode, "size", "10");
            string title = XmlHelper.GetAttributeValue(uiNode, "title", UI.Localize("Calendar"));
            string alt = XmlHelper.GetAttributeValue(uiNode, "alt");
            string appUrl =  viewContext.HttpContext.Request.ApplicationPath;
            string icon = UI.GetRelativeUrl(appUrl,UI.GetThemePath("images/icons/calendar.png")); 
            
            //Add calendar
            sb.AppendFormat("<input type=\"text\" id=\"{0}\" name=\"{1}\" class=\"input-text cm-calendar\" value=\"{2}\"  size=\"{3}\" />&nbsp;<img src=\"{4}\" class=\"cm-external-focus calendar-but\" rev=\"{0}\" title=\"{5}\" alt=\"{6}\" />",
                id,
                name,
                value,
                size,
                icon,
                title,
                alt);
            
            //Add Calendar Script
            RenderCalendarJS(sb, uiNode, id);     			
        }

        /// <summary>
        /// Render combox field
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="node"></param>
        protected void RenderComboxField(StringBuilder sb, XmlNode node)
        {
            RenderFieldLabel(sb, node);
            string value = GetValueFromDataSource(node);
            string field = XmlHelper.GetAttributeValue(node, "field");
            DataTable dtValues = GetDataTableFromDataSource(node);

            XmlNode uiNode = node.SelectSingleNode("UI");
            string textField = XmlHelper.GetAttributeValue(uiNode, "text_field");
            string valueField = XmlHelper.GetAttributeValue(uiNode, "value_field");

            sb.AppendFormat("<select name=\"{0}\" id=\"{0}\">",GenerateName(field));
            if(dtValues != null)
            {
                foreach(DataRow row in dtValues.Rows)
                {
                    sb.AppendFormat("<option value=\"{0}\" {2}>{1}</option>",
                        row[valueField].ToString(),
                        row[textField].ToString(),
                        row[valueField].ToString().Equals(value) ? "selected=\"selected\"" : "");
                }
            }
		    sb.AppendLine("</select>");
	        string tips = XmlHelper.GetNodeValue(node.SelectSingleNode("Tips"));
            if (tips.Length > 0)
                sb.AppendFormat("\"{0}\"", tips);
        }

        private static void RenderCalendarJS(StringBuilder sb, XmlNode uiNode, string id)
        {
            sb.AppendLine("<script type=\"text/javascript\">\n//<![CDATA[");
            sb.AppendFormat("var calendar_config{0} = {1};\ncalendar_config{0} = {2}\n", id, "{}", "{");

            sb.AppendFormat("changeMonth: {0},\n", XmlHelper.GetAttributeValue(uiNode, "changeMonth", "true"));
            sb.AppendFormat("duration: '{0}',\n", XmlHelper.GetAttributeValue(uiNode, "duration", "fast"));
            sb.AppendFormat("changeYear: {0},\n", XmlHelper.GetAttributeValue(uiNode, "changeYear", "true"));
            sb.AppendFormat("numberOfMonths: {0},\n", XmlHelper.GetAttributeValue(uiNode, "numberOfMonths", "1"));
            sb.AppendFormat("selectOtherMonths: {0},\n", XmlHelper.GetAttributeValue(uiNode, "selectOtherMonths", "true"));
            sb.AppendFormat("showOtherMonths: {0},\n", XmlHelper.GetAttributeValue(uiNode, "showOtherMonths", "true"));
            sb.AppendFormat("firstDay: {0},\n", XmlHelper.GetAttributeValue(uiNode, "firstDay", "1"));
            sb.AppendFormat("dayNamesMin: ['{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}'],\n",
                UI.Localize("Sun"),
                UI.Localize("Mon"),
                UI.Localize("Tue"),
                UI.Localize("Wed"),
                UI.Localize("Thu"),
                UI.Localize("Fri"),
                UI.Localize("Sat"));
            sb.AppendFormat("monthNamesShort: ['{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}', '{9}', '{10}', '{11}'],\n",
                UI.Localize("Jan"),
                UI.Localize("Feb"),
                UI.Localize("Mar"),
                UI.Localize("Apr"),
                UI.Localize("May"),
                UI.Localize("Jun"),
                UI.Localize("Jul"),
                UI.Localize("Aug"),
                UI.Localize("Sep"),
                UI.Localize("Oct"),
                UI.Localize("Nov"),
                UI.Localize("Dec"));
            sb.AppendFormat("yearRange: '{0}',\n", XmlHelper.GetAttributeValue(uiNode, "yearRange",
                string.Format("{0}:{1}", DateTime.Now.Year - 5, DateTime.Now.Year + 5)));
            sb.AppendFormat("dateFormat: '{0}'\n", XmlHelper.GetAttributeValue(uiNode, "dateFormat", "yy/mm/dd"));
            sb.AppendLine("};");
            sb.AppendLine("if($.ua.browser == 'Internet Explorer')\n{$(window).load(function(){\n");
            sb.AppendFormat("$('#{0}').datepicker(calendar_config{0});\n", id);
            sb.AppendLine("});} \nelse {$(function() {");
            sb.AppendFormat("$('#{0}').datepicker(calendar_config{0});\n", id);
            sb.AppendLine("});}");
            sb.AppendLine("//]]>\n</script>");
        }

        protected void RenderSearchListField(StringBuilder sb, XmlNode node)
        {
        }

        protected void RenderDialogSelectField(StringBuilder sb, XmlNode node)
        {
        }

        protected void RenderRadioGroupField(StringBuilder sb, XmlNode node)
        {
        }

        protected void RenderImageListTextField(StringBuilder sb, XmlNode node)
        {
        }

        protected void RenderView(StringBuilder sb, XmlNode node)
        {            
            DataListControl dl = new DataListControl(node, viewContext,viewData);
            dl.ShowInForm = true;
            sb.AppendLine(dl.GetHtml());
        }


        #endregion
    }
}
